package com.atguigu.gmall.weball.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.feign.item.SkuDetailFeignClient;
import com.atguigu.gmall.product.vo.ItemDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * @author lfy
 * @Description 商品详情 http://item.gmall.com/40.html
 * @create 2023-07-15 11:23
 */
@Controller
public class ItemController {


    @Autowired
    SkuDetailFeignClient skuDetailFeignClient;
    /**
     * 商品详情页
     * @param skuId
     * @return
     */
    @GetMapping("/{skuId}.html")
    public String item(@PathVariable("skuId") Long skuId,
                       Model model){

        // 远程调用 service-item 获取到 skuId 的所有商品详情数据


        Result<ItemDataVo> result = skuDetailFeignClient.skuDetail(skuId);
        ItemDataVo data = result.getData();
        if(data != null){
            // 把数据交给页面; 分析数据模型
            model.addAttribute("categoryView",data.getCategoryView());
            model.addAttribute("skuInfo",data.getSkuInfo());
            model.addAttribute("price",data.getPrice());
            model.addAttribute("spuSaleAttrList",data.getSpuSaleAttrList());
            model.addAttribute("valuesSkuJson",data.getValuesSkuJson());
            return "item/index";
        }


        // 跳到首页
        return "redirect:http://gmall.com";
    }

}
